package com.whlilong.jxc.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.ExtentionInfo;

public class BusinessInfoSearchByConditon extends AbstractDialog {

	private List<Hsinfo> list = new ArrayList<Hsinfo>();
	private Text businessIdText;
	private Text businessNameText;
	private Text pymText;
	private Text addressText;
	private Text telText;
	private Text peopleText;
	private Text faxText;
	private Text zipText;
	private Text mailText;
	private Text yhText;
	private Button yhCheck;
	private Button mailCheck;
	private Button zipCheck;
	private Button faxCheck;
	private Button peopleCheck;
	private Button addressCheck;
	private Button telCheck;
	private Button businessTypeCheck;
	private Button businessDistrictCheck;
	private Button pymCheck;
	private Button businessNameCheck;
	private Button businessIdCheck;
	private Button selectAllCheck;
	private Combo businessTypeCombo;
	private Combo businessDistrictCombo;
	private String businessid;
	private String businessName;
	private String pym;
	private String tel;
	private String businessDistrict;
	private String businessType;
	private String address;
	private String people;
	private String fax;
	private String zip;
	private String mail;
	private String yh;

	public BusinessInfoSearchByConditon(Shell parentShell) {
		super(parentShell);
	}

	public Control createGroupText(Composite parent) {
		FormLayout fl_parent = new FormLayout();
		fl_parent.marginTop = 20;
		fl_parent.marginRight = 5;
		fl_parent.marginLeft = 10;
		parent.setLayout(fl_parent);

		Group group = new Group(parent, SWT.NONE);
		group.setText("条件查询");
		FormLayout fl_group = new FormLayout();
		fl_group.marginRight = 5;
		fl_group.marginLeft = 5;
		fl_group.marginTop = 5;
		group.setLayout(fl_group);
		FormData fd_group = new FormData();
		fd_group.top = new FormAttachment(0, -10);
		fd_group.right = new FormAttachment(90, 20);
		fd_group.left = new FormAttachment(10, -20);
		group.setLayoutData(fd_group);

		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayout(new GridLayout(6, false));
		FormData fd_composite = new FormData();
		fd_composite.left = new FormAttachment(0);
		fd_composite.bottom = new FormAttachment(90);
		composite.setLayoutData(fd_composite);

		Label businessIdLabel = new Label(composite, SWT.NONE);
		businessIdLabel.setText("货商编号：");
		businessIdLabel.setAlignment(SWT.RIGHT);
		GridData gd_businessIdLabel = new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1);
		gd_businessIdLabel.widthHint = 75;
		businessIdLabel.setLayoutData(gd_businessIdLabel);

		businessIdText = new Text(composite, SWT.BORDER);
		businessIdText.setEnabled(false);
		GridData gd_businessIdText = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_businessIdText.widthHint = 90;
		businessIdText.setLayoutData(gd_businessIdText);

		businessIdCheck = new Button(composite, SWT.CHECK);

		GridData gd_businessIdCheck = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_businessIdCheck.widthHint = 35;
		businessIdCheck.setLayoutData(gd_businessIdCheck);

		Label businessNameLabel = new Label(composite, SWT.NONE);
		businessNameLabel.setAlignment(SWT.RIGHT);
		GridData gd_businessNameLabel = new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1);
		gd_businessNameLabel.widthHint = 75;
		businessNameLabel.setLayoutData(gd_businessNameLabel);
		businessNameLabel.setText("货商名称：");

		businessNameText = new Text(composite, SWT.BORDER);
		businessNameText.setEnabled(false);
		GridData gd_businessNameText = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_businessNameText.widthHint = 90;
		businessNameText.setLayoutData(gd_businessNameText);

		businessNameCheck = new Button(composite, SWT.CHECK);

		Label pymLabel = new Label(composite, SWT.NONE);
		pymLabel.setText("拼音码：");
		pymLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		pymText = new Text(composite, SWT.BORDER);
		pymText.setEnabled(false);
		pymText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		pymCheck = new Button(composite, SWT.CHECK);

		Label businessDistrictLabel = new Label(composite, SWT.NONE);
		businessDistrictLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		businessDistrictLabel.setText("货商区域：");

		businessDistrictCombo = new Combo(composite, SWT.READ_ONLY);
		businessDistrictCombo.add("不定");
		List districtList = ExtentionInfo.getInstance().getKeshanginfo();
		for (int i = 0; i < districtList.size(); i++) {
			Keshang ks = (Keshang) districtList.get(i);
			businessDistrictCombo.add(ks.getKeshangarea());
		}
		businessDistrictCombo.select(0);
		businessDistrictCombo.setEnabled(false);
		businessDistrictCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		businessDistrictCheck = new Button(composite, SWT.CHECK);

		Label telLabel = new Label(composite, SWT.NONE);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		telLabel.setText("联系电话：");

		telText = new Text(composite, SWT.BORDER);
		telText.setEnabled(false);
		telText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		telCheck = new Button(composite, SWT.CHECK);

		Label businessTypeLabel = new Label(composite, SWT.NONE);
		businessTypeLabel.setText("货商类别：");
		businessTypeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));

		businessTypeCombo = new Combo(composite, SWT.READ_ONLY);
		businessTypeCombo.add("不定");
		List merchantTypeList = ExtentionInfo.getInstance().getKstypeinfo();
		for (int i = 0; i < merchantTypeList.size(); i++) {
			Kstype ks = (Kstype) merchantTypeList.get(i);
			businessTypeCombo.add(ks.getKstype());
		}
		businessTypeCombo.select(0);
		businessTypeCombo.setEnabled(false);
		businessTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		businessTypeCheck = new Button(composite, SWT.CHECK);

		Label addressLabel = new Label(composite, SWT.NONE);
		addressLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		addressLabel.setText("货商地址：");

		addressText = new Text(composite, SWT.BORDER);
		addressText.setEnabled(false);
		addressText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		addressCheck = new Button(composite, SWT.CHECK);

		Label peopleLabel = new Label(composite, SWT.NONE);
		peopleLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		peopleLabel.setText("联系人：");

		peopleText = new Text(composite, SWT.BORDER);
		peopleText.setEnabled(false);
		peopleText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		peopleCheck = new Button(composite, SWT.CHECK);

		Label faxLabel = new Label(composite, SWT.NONE);
		faxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		faxLabel.setText("传真：");

		faxText = new Text(composite, SWT.BORDER);
		faxText.setEnabled(false);
		faxText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		faxCheck = new Button(composite, SWT.CHECK);

		Label zipLabel = new Label(composite, SWT.NONE);
		zipLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		zipLabel.setText("邮政编码：");

		zipText = new Text(composite, SWT.BORDER);
		zipText.setEnabled(false);
		zipText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		zipCheck = new Button(composite, SWT.CHECK);

		Label mailLabel = new Label(composite, SWT.NONE);
		mailLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		mailLabel.setText("电子邮箱：");

		mailText = new Text(composite, SWT.BORDER);
		mailText.setEnabled(false);
		mailText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		mailCheck = new Button(composite, SWT.CHECK);

		Label yhLabel = new Label(composite, SWT.NONE);
		yhLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		yhLabel.setText("银行帐号：");

		yhText = new Text(composite, SWT.BORDER);
		yhText.setEnabled(false);
		yhText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		yhCheck = new Button(composite, SWT.CHECK);

		selectAllCheck = new Button(parent, SWT.CHECK);

		FormData fd_selectAllCheck = new FormData();
		fd_selectAllCheck.top = new FormAttachment(group, 10);
		fd_selectAllCheck.right = new FormAttachment(100, -22);
		fd_selectAllCheck.bottom = new FormAttachment(100, -10);
		selectAllCheck.setLayoutData(fd_selectAllCheck);
		selectAllCheck.setText("全选(&A)");

		addListener();

		return composite;
	}

	private void addListener() {
		businessIdCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (businessIdCheck.getSelection()) {
					businessIdText.setEnabled(true);
				} else {
					businessIdText.setText("");
					businessIdText.setEnabled(false);
				}
				update();
			}
		});
		businessNameCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (businessNameCheck.getSelection()) {
					businessNameText.setEnabled(true);
				} else {
					businessNameText.setText("");
					businessNameText.setEnabled(false);
				}
				update();
			}
		});
		pymCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (pymCheck.getSelection()) {
					pymText.setEnabled(true);
				} else {
					pymText.setText("");
					pymText.setEnabled(false);
				}
				update();
			}
		});
		businessDistrictCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (businessDistrictCheck.getSelection()) {
					businessDistrictCombo.setEnabled(true);
				} else {
					businessDistrictCombo.select(0);
					businessDistrictCombo.setEnabled(false);
				}
				update();
			}
		});
		businessTypeCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (businessTypeCheck.getSelection()) {
					businessTypeCombo.setEnabled(true);
				} else {
					businessTypeCombo.select(0);
					businessTypeCombo.setEnabled(false);
				}
				update();
			}
		});
		telCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (telCheck.getSelection()) {
					telText.setEnabled(true);
				} else {
					telText.setText("");
					telText.setEnabled(false);
				}
				update();
			}
		});
		addressCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (addressCheck.getSelection()) {
					addressText.setEnabled(true);
				} else {
					addressText.setText("");
					addressText.setEnabled(false);
				}
				update();
			}
		});
		peopleCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (peopleCheck.getSelection()) {
					peopleText.setEnabled(true);
				} else {
					peopleText.setText("");
					peopleText.setEnabled(false);
				}
				update();
			}
		});
		faxCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (faxCheck.getSelection()) {
					faxText.setEnabled(true);
				} else {
					faxText.setText("");
					faxText.setEnabled(false);
				}
			}
		});
		zipCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (zipCheck.getSelection()) {
					zipText.setEnabled(true);
				} else {
					zipText.setText("");
					zipText.setEnabled(false);
				}
				update();
			}
		});
		mailCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (mailCheck.getSelection()) {
					mailText.setEnabled(true);
				} else {
					mailText.setText("");
					mailText.setEnabled(false);
				}
				update();
			}
		});
		yhCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (yhCheck.getSelection()) {
					yhText.setEnabled(true);
				} else {
					yhText.setText("");
					yhText.setEnabled(false);
				}
				update();
			}
		});
		selectAllCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (selectAllCheck.getSelection()) {
					businessIdCheck.setSelection(true);
					businessNameCheck.setSelection(true);
					pymCheck.setSelection(true);
					businessDistrictCheck.setSelection(true);
					businessTypeCheck.setSelection(true);
					telCheck.setSelection(true);
					addressCheck.setSelection(true);
					peopleCheck.setSelection(true);
					faxCheck.setSelection(true);
					zipCheck.setSelection(true);
					mailCheck.setSelection(true);
					yhCheck.setSelection(true);

				} else {
					businessIdCheck.setSelection(false);
					businessNameCheck.setSelection(false);
					pymCheck.setSelection(false);
					businessDistrictCheck.setSelection(false);
					businessTypeCheck.setSelection(false);
					telCheck.setSelection(false);
					addressCheck.setSelection(false);
					peopleCheck.setSelection(false);
					faxCheck.setSelection(false);
					zipCheck.setSelection(false);
					mailCheck.setSelection(false);
					yhCheck.setSelection(false);
				}
				initial();
			}
		});

	}

	private void initial() {
		if (businessIdCheck.getSelection()) {
			businessIdText.setEnabled(true);
		} else {
			businessIdText.setText("");
			businessIdText.setEnabled(false);
		}
		if (businessNameCheck.getSelection()) {
			businessNameText.setEnabled(true);
		} else {
			businessNameText.setText("");
			businessNameText.setEnabled(false);
		}
		if (pymCheck.getSelection()) {
			pymText.setEnabled(true);
		} else {
			pymText.setText("");
			pymText.setEnabled(false);
		}
		if (businessDistrictCheck.getSelection()) {
			businessDistrictCombo.setEnabled(true);

		} else {
			businessDistrictCombo.select(0);
			businessDistrictCombo.setEnabled(false);
		}
		if (businessTypeCheck.getSelection()) {
			businessTypeCombo.setEnabled(true);

		} else {
			businessTypeCombo.select(0);
			businessTypeCombo.setEnabled(false);
		}
		if (telCheck.getSelection()) {
			telText.setEnabled(true);
		} else {
			telText.setText("");
			telText.setEnabled(false);
		}
		if (addressCheck.getSelection()) {
			addressText.setEnabled(true);
		} else {
			addressText.setText("");
			addressText.setEnabled(false);
		}
		if (peopleCheck.getSelection()) {
			peopleText.setEnabled(true);

		} else {
			peopleText.setText("");
			peopleText.setEnabled(false);
		}
		if (faxCheck.getSelection()) {
			faxText.setEnabled(true);

		} else {
			faxText.setText("");
			faxText.setEnabled(false);
		}
		if (zipCheck.getSelection()) {
			zipText.setEnabled(true);

		} else {
			zipText.setText("");
			zipText.setEnabled(false);
		}
		if (mailCheck.getSelection()) {
			mailText.setEnabled(true);

		} else {
			mailText.setText("");
			mailText.setEnabled(false);
		}
		if (yhCheck.getSelection()) {
			yhText.setEnabled(true);

		} else {
			yhText.setText("");
			yhText.setEnabled(false);
		}
	}

	protected void update() {
		if (businessIdCheck.getSelection() && businessNameCheck.getSelection()
				&& pymCheck.getSelection()
				&& businessDistrictCheck.getSelection()
				&& businessTypeCheck.getSelection() && telCheck.getSelection()
				&& addressCheck.getSelection() && peopleCheck.getSelection()
				&& faxCheck.getSelection() && zipCheck.getSelection()
				&& mailCheck.getSelection() && yhCheck.getSelection()) {
			selectAllCheck.setSelection(true);
		} else {
			selectAllCheck.setSelection(false);
		}
	}
	protected void exeSearch(){
		businessid= businessIdText.getText();
		businessName = businessNameText.getText();
		pym = pymText.getText();
		tel = telText.getText();
		businessDistrict = businessDistrictCombo.getText();
		businessType = businessTypeCombo.getText();
		address = addressText.getText();
		people = peopleText.getText();
		fax = faxText.getText();
		zip = zipText.getText();
		mail = mailText.getText();
		yh = yhText.getText();
		
		StringBuffer hql = new StringBuffer("from Hsinfo where 1=1");
		List paramlist = new ArrayList();
		List valuelist = new ArrayList();
		int count = 0;
		if(!businessid.equals("")){
			hql.append(" and hsid=?");
			paramlist.add(count);
			valuelist.add(businessid);
			count++;
		}
		if(!businessName.equals("")){
			hql.append(" and hsname=?");
			paramlist.add(count);
			valuelist.add(businessName);
			count++;
		}
		if(!pym.equals("")){
			hql.append(" and pym=?");
			paramlist.add(count);
			valuelist.add(pym.toUpperCase());
			count++;
		}
		if(!tel.equals("")){
			hql.append(" and hstel=?");
			paramlist.add(count);
			valuelist.add(tel);
			count++;
		}
		if(businessDistrictCheck.getSelection()){
			hql.append(" and hsareaname=?");
			paramlist.add(count);
			valuelist.add(businessDistrict);
			count++;
		}
		if(businessTypeCheck.getSelection()){
			hql.append(" and hstpname=?");
			paramlist.add(count);
			valuelist.add(businessType);
			count++;
		}
		if(!address.equals("")){
			hql.append(" and address=?");
			paramlist.add(count);
			valuelist.add(address);
			count++;
		}
		if(!people.equals("")){
			hql.append(" and people=?");
			paramlist.add(count);
			valuelist.add(people);
			count++;
		}
		if(!fax.equals("")){
			hql.append(" and fax=?");
			paramlist.add(count);
			valuelist.add(fax);
			count++;
		}
		if(!mail.equals("")){
			hql.append(" and mail=?");
			paramlist.add(count);
			valuelist.add(mail);
			count++;
		}
		if(!zip.equals("")){
			hql.append(" and yzbm=?");
			paramlist.add(count);
			valuelist.add(zip);
			count++;
		}
		if(!yh.equals("")){
			hql.append(" and yh=?");
			paramlist.add(count);
			valuelist.add(yh);
			count++;
		}
		Integer[] param = new Integer[paramlist.size()];
		Object[] values = new Object[valuelist.size()];
		values = valuelist.toArray(values);
		param = (Integer[]) paramlist.toArray(param);
		list = DbManager.getInstance().search(hql.toString(),param, values);
		
	}
	public List<Hsinfo> getList() {
		return list;
	}

	public void setList(List<Hsinfo> list) {
		this.list = list;
	}
}
